package com.violet.common.response;


import com.violet.common.constant.business.CodeEnum;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 类说明:内部系统返回给前端的ajax数据封装，用于统一数据格式
 *
 * @author wqf
 * @date 2021/7/12 17:22
 */
public class Result<T> extends ResultBody<T> implements Serializable {


    public Result(boolean success, int code, String businessCode, String msg, T data) {
        super(success, code, businessCode, msg, data);
    }

    public static <T> Result<T> of(boolean success, int code, String businessCode, String msg, T data) {
        return new Result<>(success, code, businessCode, msg, data);
    }

    /**
     * 方法描述: 成功响应默认状态码
     *
     * @return com.violet.common.entity.response.Result<T>
     * @author wqf
     * @date 2022/10/25 16:01
     */
    public static <T> Result<T> succeed(String msg, T data) {
        return of(true, HttpStatus.OK.value(), CodeEnum.OK.getCode(), msg, data);
    }

    /**
     * 方法描述: 成功响应默认状态码
     *
     * @return com.violet.common.entity.response.Result<T>
     * @author wqf
     * @date 2022/10/25 16:01
     */
    public static <T> Result<T> succeed() {
        return succeed("请求成功", null);
    }

    /**
     * 方法描述: 成功响应数据和默认状态码
     *
     * @param data 响应数据
     * @return com.violet.common.entity.response.Result<T>
     * @author wqf
     * @date 2022/10/25 16:02
     */
    public static <T> Result<T> succeed(T data) {
        return succeed("请求成功", data);
    }

    /**
     * 方法描述: 成功响应消息和默认状态码
     *
     * @param msg 响应消息
     * @return com.violet.common.entity.response.Result<T>
     * @author wqf
     * @date 2022/10/25 16:01
     */
    public static <T> Result<T> succeed(String msg) {
        return succeed(msg, null);
    }

    /**
     * 方法描述: 失败响应默认状态码
     *
     * @return com.violet.common.entity.response.Result<T>
     * @author wqf
     * @date 2022/10/25 16:02
     */
    public static <T> Result<T> failure(int code, String businessCode, String msg, T data) {
        return of(false, code, businessCode, msg, data);
    }


    /**
     * 方法描述: 失败响应默认状态码
     *
     * @return com.violet.common.entity.response.Result<T>
     * @author wqf
     * @date 2022/10/25 16:02
     */
    public static <T> Result<T> failure() {
        return failure(HttpStatus.INTERNAL_SERVER_ERROR.value(), CodeEnum.SYSTEM_EXECUTION_ERROR.getCode(), "系统异常，请联系管理员", null);
    }

    /**
     * 方法描述: 失败响应消息和默认状态码
     *
     * @param msg 响应消息
     * @return com.violet.common.entity.response.Result<T>
     * @author wqf
     * @date 2022/10/25 16:03
     */
    public static <T> Result<T> failure(String msg) {
        return failure(HttpStatus.INTERNAL_SERVER_ERROR.value(), CodeEnum.SYSTEM_EXECUTION_ERROR.getCode(), msg, null);
    }

    /**
     * 方法描述: 失败响应消息和状态码
     *
     * @param code 错误码
     * @param msg  响应消息
     * @return com.violet.common.entity.response.Result<T>
     * @author wqf
     * @date 2022/10/25 16:03
     */
    public static <T> Result<T> failure(int code, String businessCode, String msg) {
        return failure(code, businessCode, msg, null);
    }

    /**
     * 方法描述: 失败响应消息和状态码
     *
     * @param businessCode 错误码
     * @param msg       响应消息
     * @return com.violet.common.entity.response.Result<T>
     * @author wqf
     * @date 2022/10/25 16:03
     */
    public static <T> Result<T> failure(String businessCode, String msg) {
        return failure(HttpStatus.INTERNAL_SERVER_ERROR.value(), businessCode, msg, null);
    }
}
